<?php

namespace app\common\service;

/**
 * 公共服务
 */
class CommonService {

    /**
     * @var int 状态码
     */
    const CODE_KEY = 'code';

    /**
     * @var string 消息
     */
    const MSG_KEY = 'msg';

    /**
     * @var array 结果集
     */
    const DATA_KEY = 'data';
    
    /**
     * 操作成功
     * @var integer
     */
    const SUCCESS_CODE     = 200;
    const SUCCESS_MSG = '操作成功';
    
    /**
     * 操作失败
     * @var integer
     */
    const ERROR__CODE     = 400;
    const ERROR_MSG = '操作失败';

    /**
     * @var mixed 静态变量
     */
    private static $code, $msg, $data;

    /**
     * @var array 返回数据
     */
    private static $_data = [
        self::CODE_KEY => self::SUCCESS_CODE,
        self::MSG_KEY => self::SUCCESS_MSG,
        self::DATA_KEY => []
    ];

    /**
     * 设置错误码
     * @param int $code
     * @return \self
     */
    public static function code($code = self::SUCCESS_CODE) {
        self::$code = self::$_data[self::CODE_KEY] = $code;
        return new self;
    }

    /**
     * 设置提示语
     * @param string $msg
     * @return \self
     */
    public static function msg($msg = self::SUCCESS_MSG) {
        self::$msg = self::$_data[self::MSG_KEY] = $msg;
        return new self;
    }

    /**
     * 设置数据
     * @param array $data
     * @return \self
     */
    public static function data($data = []) {
        self::$data = self::$_data[self::DATA_KEY] = $data;
        return new self;
    }

    /**
     * 输出
     * @return array
     */
    public static function out() {
        return self::$_data;
    }
    
    /**
     * 输出成功提示
     * @param ayyay $data 返回数据
     * @param string $msg 消息提示
     * @param string $code 状态码
     * @return array
     */
    public static function success_out($data = [], $msg = self::SUCCESS_MSG, $code = self::SUCCESS_CODE) {
        $ret = [self::CODE_KEY => $code];
        $msg != '' && $ret[self::MSG_KEY] = $msg;
        $ret[self::DATA_KEY] = $data;
        return self::ajaxReturn_out($ret);
    }
    
    /**
     * 输出错误提示
     * @param string $msg 消息提示
     * @param array $data 返回数据
     * @param string $code 状态码
     * @return array
     */
    public static function error_out($msg = self::ERROR_MSG, $data = [], $code = self::ERROR__CODE) {
        $ret = [self::CODE_KEY => $code];
        $msg != '' && $ret[self::MSG_KEY] = $msg;
        !empty($data) && $ret[self::DATA_KEY] = $data;
        return self::ajaxReturn_out($ret);
    }

    /**
     * 输出错误提示
     * @param array $data 返回数据
     * @param string $msg 消息提示
     * @param string $code 状态码
     * @return mixed
     */
    protected static function success($data = [], $msg = self::SUCCESS_MSG, $code = self::SUCCESS_CODE) {
        $ret = [self::CODE_KEY => $code];
        $msg != '' && $ret[self::MSG_KEY] = $msg;
        $ret[self::DATA_KEY] = $data;
        self::ajaxReturn($ret);
    }

    /**
     * 输出错误提示
     * @param string $msg 消息提示
     * @param array $data 返回数据
     * @param string $code 状态码
     * @return mixed
     */
    protected static function error($msg = self::ERROR_MSG, $data = [], $code = self::ERROR__CODE) {
        $ret = [self::CODE_KEY => $code];
        $msg != '' && $ret[self::MSG_KEY] = $msg;
        !empty($data) && $ret[self::DATA_KEY] = $data;
        self::ajaxReturn($ret);
    }

    /**
     * 返回响应数据
     * @param string|array $data
     * @throws \think\exception\HttpResponseException
     */
    protected static function ajaxReturn($data = []) {
        print_r(json_encode($data, JSON_UNESCAPED_UNICODE));
        exit;
    }
    
    /**
     * 返回响应数据
     * @param string|array $data
     * @throws \think\exception\HttpResponseException
     */
    protected static function ajaxReturn_out($data = []) {
//        return json($data);
        return $data;
    }

    /**
     * 查询构造器
     * @param string $table
     * @return \think\db\Query
     */
    protected static function query($table) {
        return \app\common\model\Base::instance($table);
    }

    /**
     * 模型对象
     * @return \think\db\Model
     */
    protected static function model() {
        return \app\common\model\Base::instance();
    }
    
    /**
     * 应用模块
     */
    public static function getModule() {
        return \think\facade\Request::instance()->module();
    }
    
    /**
     * 应用控制器
     */
    public static function getController() {
        return \think\facade\Request::instance()->controller();
    }
    
    /**
     * 表名
     * @param array $data 数据
     * @return int
     */
    protected static function getTable() {
        if (isset(static::$table)) {
            $table = static::$table;
        } else {
            $class_arr = explode('\\', get_called_class());
            $class = end($class_arr);
            $arr = preg_split('/(?=[A-Z])/', lcfirst($class));
            array_pop($arr);
            $table = join(array_map('strtolower', $arr), '_');
        }
        return $table;
    }

    /**
     * 主键
     * @param array $data 数据
     * @return int
     */
    protected static function getTablePk() {
        if (isset(static::$table_pk)) {
            $table_pk = static::$table_pk;
        } else {
            $table_pk = ($pos = strripos(self::getTable(), '_')) ? substr(self::getTable(), $pos + 1) . '_id' : self::getTable() . '_id';
        }
        return $table_pk;
    }
    
    /**
     * ID
     * @param array $data 数据
     * @return int
     */
    protected static function ID($data) {
        return $data['id'] ?? ($data[self::getTablePk()] ?? 0);
    }
    
    /**
     * 销毁数据
     * @param array $data 数据
     * @param string $key 指定键
     */
    protected static function unsetData(&$data = [], $key = null) {
        if ($data && $key) {
            if (!stripos($key, ',')) {
                unset($data[$key]);
            } else {
                foreach (explode(',', $key) as $k) {
                    unset($data[$k]);
                }
            }
        }
    }

}
